package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author: atguigu
 * @create: 2023-03-13 10:27
 */
@Slf4j
@Component
public class DelayMsgReceiver {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 监听延迟插件实现-延迟消息
     *
     * @param msg
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {DelayedMqConfig.queue_delay_1})
    public void processDeadLetter(String msg, Message message, Channel channel) {
        try {
            if (StringUtils.isNotBlank(msg)) {
                //控制同样一个消息只能被消费一次:增加幂等性处理机制
                String key = "mq:" + msg;
                Boolean flag = redisTemplate.opsForValue().setIfAbsent(key, "", 10, TimeUnit.MINUTES);
                if (!flag) {
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    return;
                }
                log.info("监听到消息:{}", msg);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("监听延迟消息异常:{}", e);
        }
    }
}
